﻿package effects
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.events.*;
	import flash.external.ExternalInterface;
	import flash.geom.ColorTransform;
	import flash.geom.Matrix;
	import flash.geom.Transform;
	import flash.net.URLRequest;
	import flash.utils.clearInterval;
	import flash.utils.setInterval;
	public class Effect3 extends Effect
	{
		private var da:Number;
		private var w:int;
	 	private var h:int;	
	 	private var url:String = "assets/drawer.swf";
	    private var loader:Loader = new Loader();
	    private var loaderc:Loader = new Loader();
	    private var d:*;
	    private var c:*;
	   	
	   	private var matrix:Matrix ;
	   	private var inter:Number;
	   	
	   	private var dx:Number;
	   	private var dy:Number;
	   	
	   	private var cx:Number;
	   	private var cy:Number;
	   	 
	   	private var t:Number;
	   	private var tc:Number;
	   		
	   	private var bmpd:Bitmap;
	   	private var bmp:BitmapData;
	   	
	   	private var bmpc:Bitmap;
	   	private var bmc:BitmapData;
		
		public function Effect3()
		{
			tc=t=0;
			
			da=1;
			cacheAsBitmap=true;
			var request:URLRequest = new URLRequest(url);
            
		    loader.load(request);
		    loaderc.load(request);
		
		    loader.contentLoaderInfo.addEventListener(Event.COMPLETE, draw);
		    loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
		}
		public override function add():void
		{
			d.addEventListener(Event.ENTER_FRAME,renderd);
			c.addEventListener(Event.ENTER_FRAME,renderc);
		}
		public override function remove():void
		{
			
			c.removeEventListener(Event.ENTER_FRAME,renderc);
			d.removeEventListener(Event.ENTER_FRAME,renderd);
			bmp.fillRect(bmp.rect,0xFF000000);
			bmc.fillRect(bmc.rect,0x00000000);
			c.x=d.x=(Global._stage.stageWidth/2);
			c.y=d.y=(Global._stage.stageHeight/2);
			t=tc=0;
		}
		private function draw(event:Event):void 
		{
			bmp = new BitmapData(Global._stage.stageWidth,Global._stage.stageHeight,true,0xFF000000);
			bmc = new BitmapData(Global._stage.stageWidth,Global._stage.stageHeight,true,0x00000000);
			bmpd = new Bitmap(bmp,"auto",true);
			bmpc = new Bitmap(bmc,"auto",true);
			
			addChild(bmpd);
			addChild(bmpc);

			d = loader;
			c = loaderc;
			c.x=d.x=(Global._stage.stageWidth/2);
			c.y=d.y=(Global._stage.stageHeight/2);
			c.scaleY=c.scaleX=2;
			
			cx=dx=(Global._stage.stageWidth/2);
			cy=dy=(Global._stage.stageHeight/2);
			
			
			d.addEventListener(Event.ENTER_FRAME,renderd);
			inter = setInterval(renstartc,7000);
		}
		private function renstartc():void
		{
			clearInterval(inter);
			c.addEventListener(Event.ENTER_FRAME,renderc);
		}
		private function renderc(e:Event):void
		{
			var rad:Number = 150;
			c.x = ((tc/5)*Math.cos(Math.PI * tc/180))+cx;
			c.y = ((tc/5)*Math.sin(Math.PI * tc/180))+cy;
			tc++;
			bmc.draw(c,c.transform.matrix,new ColorTransform(0,0,0,1,0,0,0,1));
		}
		private function renderd(e:Event):void
		{
			var rad:Number = 150;
			d.x = ((t/5)*Math.cos(Math.PI * t/180))+dx;
			d.y = ((t/5)*Math.sin(Math.PI * t/180))+dy;
			t+=2;
			bmp.draw(d,d.transform.matrix);
		}
		private function ioErrorHandler(event:IOErrorEvent):void {
			ExternalInterface.call("alert","Unable to load image: " + url);
		}
	}
}